#!/bin/sh
#目录说明：wifi-pay-sdk和zhangxin/social_main在同一级目录
#使用方法：./lxpay.sh

sdkPath=`pwd`

cd ../
if [[ ! -d "zhangxin" ]]; then
  echo "--->>>no dir zhangxin"
  exit 1
fi
#停止更新连信代码，防止连信依赖库变更后，下载依赖耗时太长
#cd zhangxin
#if [[ -d ".git" ]]; then
#  #更新连信代码
#  git add social_main/app
#  git stash
#  git pull --rebase
#  git stash pop
#fi

cd ${sdkPath}
git pull --rebase
./gradlew clean LXPR
buildStatus=$?
if [[ ! ${buildStatus} = 0 ]]; then
	exit 1
fi

#获取最新的aar
aarPath=`ls -t SdpOpenWallet/build/AAR/*.aar |head -1`
echo "--->>>new aar path: ${aarPath}"
aarFile=`basename "${aarPath}"`
aarFileWithoutExtension=${aarFile%.*}

lxProject="../zhangxin/social_main"
lxLibs="${lxProject}/app/libs"
aarPathOld=`find ${lxLibs} -name 'LXProduct*'`
aarFileOld=`basename "${aarPathOld}"`
aarFileWithoutExtensionOld=${aarFileOld%.*}
echo "--->>>old aar file: ${aarFileWithoutExtensionOld}"

#替换.aar
rm -rf ${aarPathOld}
cp ${aarPath} ${lxLibs}

cd ${lxProject}
#修改build.gradle中aar的文件名
OS=`uname`
if [[ ${OS} == "Darwin" ]]; then
	sed -i "" "s/${aarFileWithoutExtensionOld}/${aarFileWithoutExtension}/g" app/build.gradle
else 
	sed -i "s/${aarFileWithoutExtensionOld}/${aarFileWithoutExtension}/g" app/build.gradle
fi
if [[ ! $? = 0 ]]; then
	exit 1
fi

#检验build.gradle中aar文件名是否替换成功
check=`grep ${aarFileWithoutExtension} app/build.gradle`
if [[ -z ${check} ]]; then
	exit 1
fi
echo "--->>>new aar file: ${aarFileWithoutExtension}"
echo "--->>>build.gradle: ${check}"

./gradlew clean assembleDev
buildStatus=$?
if [[ ! ${buildStatus} = 0 ]]; then
	exit 1
fi

lxApkPath=`ls -t app/build/outputs/apk/*.apk |head -1`
echo "--->>>lx apk path in social_main: ${lxApkPath}"
